/**
 * 数据导出
 * @returns {boolean}
 */
function exportInfo() {
    // debugger
    var checkResult = checkHasSybAuth($('#signOrgId option'));
    var params = $("#titleSearch").serializeObject();
    var isNotNull = 0;
    $.each(params, function (i, item) {
        if (!checkIsEmpty(item)) {
            isNotNull = isNotNull + 1;
            return false;
        }
    });
    if (isNotNull === 0) {
        DGG.Errors("请选择搜索条件后进行导出!");
        return false;
    }
    if (!checkResult) {
        params["isAll"]=0;
    }
    export_table("/zs/general_credential/export_credential.do", params);
}

/**
 * 出库
 * @param id
 */
function getOut(id) {
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/general_credential/check_library_status.do", {
        id: id,
        libraryStatus: 'GENERAL_CREDENTIAL_LIBRARY_STATUS_01'
    }, function (result) {
        if (result.code == 0) {
            $.post(sysInfo.basePath + "/zs/general_credential/get_out_page.html", {id: id}, function (data) {
                var index = dgg.Open({
                    title: '证书出库',
                    btn: ["确定", "取消"],
                    content: data,
                    area: '500px',
                    yes: function () {
                        DGG.loading();
                        if (!checkCertificateFileds()) {
                            DGG.loading('close');
                            return false;
                        }
                        var params = $("#credentialForm").serializeObject();
                        params.id = id;
                        $.post(sysInfo.basePath + "/zs/general_credential/get_out.do", params, function (result) {
                            if (result.code == 0) {
                                layer.closeAll();
                                DGG.Succeed('出库成功');
                                window.location.reload();
                            } else {
                                DGG.Errors(result.msg);
                                DGG.loading('close');
                                return false;
                            }
                        });

                    }, end: function () {
                        layer.closeAll("tips");
                        DGG.loading('close');
                    }
                });
            }, 'html');
        } else {
            DGG.Errors(result.msg);
            DGG.loading('close');
            return false;
        }

    });
}


/**
 * 入库
 * @param id
 */
function getIn(id) {
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/general_credential/check_library_status.do", {
        id: id,
        libraryStatus: 'GENERAL_CREDENTIAL_LIBRARY_STATUS_02'
    }, function (result) {
        if (result.code == 0) {
            $.post(sysInfo.basePath + "/zs/general_credential/get_in_page.html", {id: id}, function (data) {
                var index = dgg.Open({
                    type: 1,
                    title: '证书入库',
                    btn: ["确定", "取消"],
                    content: data,
                    area: '500px',
                    yes: function () {
                        DGG.loading();
                        if (!checkDropDownChoice('#warehousingReason', $('#warehousingReason').next())) {
                            DGG.loading('close');
                            return false;
                        }
                        if (!checkDropDownChoice('#certificateUserId', "[name='checkCertificateUserId']", '')) {
                            DGG.loading('close');
                            return false;
                        }
                        var params = $("#credentialForm").serializeObject();
                        params.id = id;
                        $.post(sysInfo.basePath + "/zs/general_credential/get_in.do", params, function (result) {
                            if (result.code == 0) {
                                layer.closeAll();
                                DGG.Succeed('入库成功');
                                window.location.reload();
                            } else {
                                DGG.Errors(result.msg);
                                DGG.loading('close');
                                return false;
                            }
                        });

                    }, end: function () {
                        layer.closeAll("tips");
                        DGG.loading('close');
                    }
                });
            }, 'html');
        } else {
            DGG.Errors(result.msg);
            DGG.loading('close');
            return false;
        }

    });
}

/**
 * 证书需要验证的字段
 */
function checkCertificateFileds() {
    // 验证下拉选择框
    if (!checkDropDownChoice('#outgoingReason', $('#outgoingReason').next(), '')) return false;
    if (!checkDropDownChoice('#certificateUserId', "[name='checkCertificateUserId']", '')) return false;
    // 验证输入框 outboundDataType
    if (!checkInputBox('[name="outboundDataType"]:checked', '请选择出库资料类型', '[name="checkOutboundDataType"]')) return false;

    return true;
}

/**
 * 费用申请
 * @param id
 */
function applyMoneyByCertificate(id) {
    $.post(sysInfo.basePath + "/zs/general_credential/find_order_business_by_certificate.do", {id: id}, function (result) {
        if (result.code == 0) {
            if (checkIsEmpty(result.data.orderBusinessId)) {
                DGG.Errors("旧的证书数据不能进行费用申请！");
                return false;
            }
            // window.parent.updateTab("费用申请：" + result.data.orderBusinessNo, orfApiUrl + "/orf/outlay/addoutlay.html?orderBusinessId=" + result.data.orderBusinessId + "&type=1");
            // 新的费用申请
            window.parent.updateTab('费用申请:' + result.data.orderBusinessNo, orfNewApiUrl + "/outlay/apply_outlay.html");
        } else {
            DGG.Errors("系统错误，请联系管理员！");
        }
    });
}

/**
 * 导出全部数据
 */
function exportAllInfo() {
    var params = $("#titleSearch").serializeObject();
    export_table("/zs/general_credential/export_all_credential.do", params);
}

/**
 * 使用证书
 */
function affiliatedCredential() {
    var ids = getTableSelect("#myTable");
    if (ids.length == 0) {
        DGG.Warning('请选择使用证书!');
        return false;
    }
    if (ids.length > 1) {
        DGG.Warning('只能选择单个证书进行使用!');
        return false;
    }
    var certificateId = ids[0];
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/general_credential/check_credential_status.do", {certificateId: certificateId}, function (result) {
        if (result.code == 0) {
            $.post(sysInfo.basePath + "/zs/general_credential/affiliated_certificate_page.html", {certificateId: certificateId}, function (data) {
                 dgg.Open({
                    title: '使用证书',
                    content: data,
                    area: '500px',
                    btn: ["确定", "取消"],
                    yes: function () {
                        DGG.loading('close');
                        confirmAffiliated(certificateId)
                    }
                });
            }, 'html');
        } else {
            DGG.Errors(result.msg);
            DGG.loading('close');
        }
    });
}

// 清空搜索
function cleanSearchWithHidden() {
    var $s = $('.titleSearch');
    $s.find('input').val('');
    $s.find('select').val('');
    $s.find('select').val('').trigger("change");
    $s.find('input').attr("data-id", '');
    $.each($s.find("input[type=hidden]"), function (i, v) {
        $(this).val('');
    });
}

/**
 * 证书更改状态
 */
function cancelStatus() {
    var ids = getTableSelect("#myTable");
    if (checkTableSelect(ids, 0)) {
        DGG.loading()
        $.post(sysInfo.basePath + '/zs/general_credential/cancel_certificate_status.do', {certificateId: ids[0]}, function (result) {
            if (result.code == 0) {
                DGG.Succeed('更改状态成功！');
                window.location.reload();
            } else {
                DGG.Errors(result.msg);
            }
            DGG.loading('close');

        }, 'json');
    }
}
/**
 * 企业续签
 */
function businessRenewal() {
    var ids = getTableSelect("#myTable");
    if (ids.length == 0) {
        DGG.Warning('请选择使用证书!');
        return false;
    }
    if (ids.length > 1) {
        DGG.Warning('只能选择单个证书进行使用!');
        return false;
    }
    var certificateId = ids[0];
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/general_credential/check_business_status.do", {certificateId: certificateId}, function (result) {
        if (result.code == 0) {
            $.post(sysInfo.basePath + "/zs/general_credential/business_renewal.html", {certificateId: certificateId}, function (data) {
                dgg.Open({
                    title: '企业续签',
                    content: data,
                    area: '500px',
                    btn: ["确定", "取消"],
                    yes: function () {
                        DGG.loading('close');
                        confirmAffiliated(certificateId)
                    }
                });
            }, 'html');
        } else {
            DGG.Errors(result.msg);
            DGG.loading('close');
        }
    });
}

/**
 *人才续签
 */
function talentRenewal(id) {
    var ids = getTableSelect("#myTable");
    var certificateId = ids[0];
    DGG.loading();
    var talentRenewal='talentRenewal'
    $.post(sysInfo.basePath + "/zs/general_credential/talent_renewal.html", {certificateId: certificateId,id: id}, function (data) {
        dgg.Open({
            title: '人才续签',
            content: data,
            area: '90%',
            btn: ["确定", "取消"],
            yes: function () {
                DGG.loading('close');
                confirmAffiliated1(talentRenewal)
            },
            end: function () {
                location.reload()
            }
        });
    }, 'html');
}

function updateCertificateStatus() {
    var ids = getTableSelect("#myTable");
    if (ids.length == 0) {
        DGG.Warning('请选择需要调整的证书!');
        return false;
    }
    if (ids.length > 10) {
        DGG.Warning('最多只能同时操作10本证书!');
        return false;
    }
    DGG.Confire("确定要要批量修改状态？？", '修改证书状态', function () {
        DGG.loading();
        $.ajax({
            type: 'POST',
            async: true,
            data: {"ids": JSON.stringify(ids)},
            url: sysInfo.basePath + '/zs/general_credential/update_certificate_status.do',
            dataType: 'JSON',
            success: function (data) {
                DGG.loading('close');
                if (data.code == 0) {
                    DGG.Succeed('修改成功！')
                    setTimeout(function () {
                        layer.closeAll();
                    }, 1000);
                    window.location.reload();
                } else {
                    DGG.Errors(data.msg);
                }
            },
            error: function () {
                DGG.Errors('删除失败，请联系管理员！！');
            }
        })
    }, function () {
    })
}